package com.mf.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;

@Data
@Schema(description = "响应结果实体类")
@Accessors(chain = true) // 引入 lombok 链式调用
public class Result<T> {

    //返回码
    @Schema(description = "业务状态码")
    private Integer status;

    //返回消息
    @Schema(description = "响应消息")
    private String message;

    //返回数据
    @Schema(description = "业务数据")
    private T data;

    // 防止请求缓存
    @Schema(description = "防止请求缓存")
    private  long timestamp;

    // 私有化构造
    private Result() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 系统成功的返回
     * @param data 返回信息
     * @return Result
     * @param <T> 类型
     */
    public static <T> Result<T> success(T data){
        final Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }
    public static <T> Result<T> success(){
        final Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        result.setData(null);
        return result;
    }


    /**
     * 系统成功的返回
     * @param data 返回信息
     * @return Result
     * @param <T> 类型
     */
    public static <T> Result<T> success(T data ,ResultCodeEnum resultCodeEnum){
        final Result<T> result = new Result<>();
        result.setStatus(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(data);
        return  result;
    }

    /**
     * 系统错误返回
     * @return Result
     * @param <T> 类型
     */
    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum){
        final Result<T> result = new Result<>();
        result.setStatus(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(null);
        return  result;
    }


    /**
     * 系统错误返回
     * @return Result
     */
    public static  Result<String> fail(String message){
        final Result<String> result = new Result<>();
        result.setMessage(message);
        result.setStatus(4000);
        result.setData(null);
        return  result;
    }

    /**
     * 系统错误返回
     * @return Result
     */
    public static <T>  Result<T> fail(){
        final Result<T> result = new Result<>();
        result.setMessage("操作失败");
        result.setStatus(4000);
        result.setData(null);
        return  result;
    }

}
